/**
 * Created by Administrator on 2016/8/2.
 */
"use strict";
swan_pc
    .controller("AddEventController",function($scope,$timeout,ngDialog,$q,EventManage,candidateUserManage,ContactInfo,GetAccountInfo){
        $scope.newEvent=
        {
            "activityType": "INDIVIDUALEVENT",
            "address": null,
            "agenda": null,
            "dressCode": null,
            "endTime": null,
            "startTime": null,
            "limitNumber": 10,
            "name": null,
            "prerequisite":null,
            "remark": null,
            "stationeryUrl":null,
            "status": "DRAFT",
            "type": null
        }
        $scope.candidateUser={
            activityId : null ,
            activityType :"INDIVIDUALEVENT",
            approveStatus : null, //邀请者批准状态 = ['PENDING', 'WAITING', 'CONFIRM', 'RESERVE', 'REJECT', 'CANCEL', 'FAIL'],
            approveTime:null, //邀请者批准时间 ,
            candidateId:null, //被邀请者的Member Id或UserContact Id ,
            candidateType:"USER_CONTACT", //被邀请者类型 = ['MEMBER', 'USER_CONTACT'],
            content : null,//内容，可自行填写；该字段为null时，后台自动填写对应活动的名称 ,
            enrollStatus: null,//被邀请者enroll状态 = ['PENDING', 'CONFIRM', 'REJECT', 'CANCEL', 'CONFLICT', 'FAIL'],
            enrollTime:null, //被邀请者enroll时间 ,
            hasUpdate:null, //是否有更新 ,
            inviter:null,    //邀请者，可自行填写；该字段为null时，后台自动填写组织名或邀请者姓名 ,
            lastRemindTime: null,//上一次提醒被邀请者的时间 ,
            //parentId (integer, optional),
            userId:null //被邀请者user id，由后台填写
        }
        //撤销信息重新填写
        $scope.resetMessage=function(){
            $scope.newEvent=
            {
                "activityType": "INDIVIDUALEVENT",
                "address": null,
                "agenda": null,
                "dressCode": null,
                "endTime": null,
                "startTime": null,
                "enrollType": "FIRST_COME_FIRST_SERVED",
                "limitNumber": 10,
                "name": null,
                "prerequisite":null,
                "remark": null,
                "stationeryUrl":null,
                "status": "DRAFT",
                "type": null
            }
            $scope.memberInvited=[];
        }
        //活动创建及人员邀请
        $scope.currentActivityId=0;
        $scope.candidateUserInvite=function(i){
            $scope.candidateUser.candidateId=$scope.memberInvited[i];
            candidateUserManage.save($scope.candidateUser,function(result){
                //console.log(result);
                if(i<$scope.memberInvited.length-1){
                    i=i+1;
                    $scope.candidateUserInvite(i);
                }
            },function(result){
                console.log(result);
            })
        }
        $scope.eventSubmit=function(){
            var def=$q.defer();
            var pro=def.promise;
            EventManage.save($scope.newEvent,function(result){
                $scope.currentActivityId=result.id;
                def.resolve();
            },function(result){
                console.log(result);
            })
            pro.then(function(){
                $scope.candidateUser.activityId=$scope.currentActivityId;
                $scope.candidateUserInvite(0);
                swal("活动创建成功","","success");
            })
        }
        //contact选择
        $scope.memberInvited=[];
        $scope.contactCurrentInfo=[];
        $scope.contactNameList="";
        ContactInfo.query(function(result){
            $scope.contactCurrentInfo=result;
        })
        $scope.$watch(function(){
            return $scope.memberInvited;
        },function(){
            $scope.contactNameShow($scope.memberInvited);
        })
        $scope.contactNameShow=function(idlist){
            var contactNameList="";
            for(var i=0;i<idlist.length;i++){
                for(var j=0;j<$scope.contactCurrentInfo.length;j++){
                    if(idlist[i]==$scope.contactCurrentInfo[j].id){
                        contactNameList=contactNameList+","+$scope.contactCurrentInfo[j].baseInfo.name;
                    }
                }
            }
            var length=contactNameList.length;
            contactNameList=contactNameList.substring(1,length);
            $scope.contactNameList=contactNameList;
        }
        $scope.choseContact=function(){
            ngDialog.open({
                templateUrl:"scripts/components/yue/organizebyme/dialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.selectedContact=[];
                    $scope.inputContactName="";
                    $scope.selectFlag=0;
                    $scope.ContactConfirm=function(){
                        $scope.$parent.memberInvited=$scope.selectedContact;
                        //console.log($scope.$parent.memberInvited);
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        //当前account获取
        GetAccountInfo.get(function(result){
                $scope.presentAccountInfo=result;
                $scope.log_username=$scope.presentAccountInfo.firstName;
            },
            function(result){
            })
        //信纸列表选取
        $scope.stationeryUrlList={
            "信纸1":"http://www.diamondtiming.com:8080/uploads/20160809131748_xin0.jpg",
            "信纸2":"http://www.diamondtiming.com:8080/uploads/20160809131803_xin1.jpg",
            "信纸3":"http://www.diamondtiming.com:8080/uploads/20160809131809_xin2.jpg",
            "信纸4":"http://www.diamondtiming.com:8080/uploads/20160809131815_xin3.jpg",
            "信纸5":"http://www.diamondtiming.com:8080/uploads/20160809131823_xin4.jpg"
        }
        $scope.stationeryState=-1
        $scope.stationerySelect=function(id){
            //console.log("stationerySelectRun");
            $scope.newEvent.stationeryUrl="信纸"+id;
            $scope.stationeryState=id;
        }
        //邀请函预览
        $scope.addOrPreview=0;
        $scope.continueEdit=function(){
            $scope.addOrPreview=0;
        }
        $scope.perviewInvitation=function(){
            $scope.addOrPreview=1;
            for(var key in $scope.stationeryUrlList){
                //console.log(key + "   "+$scope.newEvent.stationeryUrl);
                if($scope.newEvent.stationeryUrl==key){
                    var imageUrl=$scope.stationeryUrlList[key];
                    var imageUrl=imageUrl.split(".");
                    var imageUrlLength=imageUrl[2].length;
                    var imageFlag=imageUrl[2][imageUrlLength-1];
                    if(imageFlag==0){
                        $scope.invationStyle = {
                            "position": 'center',
                            "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                            "background-size": 'cover',
                            "width": '100%',
                            "padding":"20% 0"
                        }
                    }else if(imageFlag==1){
                        $scope.invationStyle = {
                            "position": 'center',
                            "background-color":(0,152,50),
                            "opacity":0.2,
                            "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                            "background-size": 'cover',
                            "width": '100%',
                            "padding":"20% 0",
                            "color":"white",
                            "text-shadow": "5px 5px 5px black"
                        }
                    } else if(imageFlag==2) {
                        $scope.invationStyle = {
                            "position": 'center',
                            "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                            "background-size": 'cover',
                            "width": '100%',
                            "padding":"30% 0",
                            "color":"white",
                        }
                    } else if(imageFlag==3){
                        $scope.invationStyle = {
                            "position": 'center',
                            "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                            "background-size": 'cover',
                            "width": '100%',
                            "padding":"20% 0",
                            "color":"white"
                        }
                    } else{
                        $scope.invationStyle = {
                            "position": 'center',
                            "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                            "background-size": 'cover',
                            "width": '100%',
                            "padding":"20% 0",
                            "color":"#EEC900",
                            "text-shadow": "3px 3px 3px blue"
                            //"filter":"shadow(Color=#0000ff,Direction=10,Strength=10)"
                        }
                    }
                    //console.log($scope.invationStyle);
                }
            }
        }
        $scope.setDivHeight=function(){
            var w=$('#invationContent').width();
            //$('#invationContent').attr('height',w*1.77);
            var height=w*1.3;
            document.getElementById("invationContent").style.height=height+"px";
            var h=$('#invationContent').height();
            //console.log("run: "+w);
            //console.log("height:"+h);
        }
        $scope.setDivHeight();
    })
